*************
** Table 3 **
*************

global root "D:\Dropbox\unequal_gains\QJE revision plan\analysis\CEX"
global rootcpi "D:\Dropbox\unequal_gains\QJE revision plan\analysis\CPI"
global resultspath "D:\Dropbox\unequal_gains\QJE revision plan\analysis\clean_results"

* (1) Detailed Categories
* number of categories
use "$rootcpi\cpi_cex_2004_2015_final", clear
keep series_id
duplicates drop
gen N=[_N]
keep N
gen AggregationLevel="Detailed Categories"
duplicates drop
save "$resultspath\Table3.dta", replace
* Tornqvist inflation difference across detailed categories
use "$rootcpi\results", clear
keep if price_index==1
rename index infl_diff
gen AggregationLevel="Detailed Categories"
keep Aggregation infl
merge 1:1 Aggregation using "$resultspath\Table3.dta"
drop _merge
save "$resultspath\Table3.dta", replace

* (2) Subcategories
use "$rootcpi\cpi_cex_2004_2015_final", clear
keep Subcategory
duplicates drop
gen N=[_N]
keep N
gen AggregationLevel="Subcategories"
duplicates drop
append using "$resultspath\Table3.dta"
save "$resultspath\Table3.dta", replace

* Tornqvist inflation difference across subcategories
use  "$rootcpi\cpi_cex_2004_2015_final", clear

rename spending spending_all

distinct Sub
bysort year Sub: egen double tot_spending_all_sub=sum(spending_all)
gen s_item=spending_all/tot_spending_all_sub
bysort year Sub: egen infl_main=sum(s_item*infl)

foreach i in spending_all spending_incq1 spending_incq2 spending_incq3 spending_incq4 spending_incq5 {
bysort year: egen double tot_`i'=sum(`i')
gen s_`i'=`i'/tot_`i'
}

egen id=group(series_id)
tsset id year

foreach i in spending_all spending_incq1 spending_incq2 spending_incq3 spending_incq4 spending_incq5 {
gen so_`i'=L.s_`i'
}

* Compute price indices
gen price_ratio=infl_main+1
replace infl=infl_main
foreach i in all incq1 incq2 incq3 incq4 incq5 {
* laspeyres * paasche
bysort year: egen laspeyres_`i'=sum(infl*so_spending_`i')
bysort year: egen paasche_`i'=sum(infl*s_spending_`i')
* tornqvist
gen weight_`i'=1/2*(s_spending_`i'+so_spending_`i')
gen double numerator_temp_`i'=weight_`i'*log(price_ratio)
bysort year: egen double numerator_`i'=sum(numerator_temp_`i')
gen double tornqvist_`i'=exp(numerator_`i')
* express as inflation in % (note that for Tornqvsit we need -1 since we build from the price ratio)
replace laspeyres_`i'=laspeyres_`i'*100
replace paasche_`i'=paasche_`i'*100
replace tornqvist_`i'=(tornqvist_`i'-1)*100
}

keep year las* paa* tor*
duplicates drop

drop if year==2004
collapse (mean) *
drop *_all 
reshape long laspeyres_incq paasche_incq tornqvist_incq, i(year) j(income_quintile)
drop year

keep if income_quintile==1 | income_quintile==5
gsort - inc
gen infl_diff=tornqvist_incq-tornqvist_incq[_n-1]
drop if missing(infl_diff)
keep infl_diff
gen AggregationLevel="Subcategories"
merge 1:1 Aggregation using "$resultspath\Table3.dta"
drop _merge
save "$resultspath\Table3.dta", replace

* (3) Main Categories
use "$rootcpi\cpi_cex_2004_2015_final", clear
keep Main
duplicates drop
gen N=[_N]
keep N
gen AggregationLevel="Main Categories"
duplicates drop
append using "$resultspath\Table3.dta"
save "$resultspath\Table3.dta", replace

* Tornqvist inflation difference across categories

use  "$rootcpi\cpi_cex_2004_2015_final", clear

rename spending spending_all

distinct Main
bysort year Main: egen double tot_spending_all_main=sum(spending_all)
gen s_item=spending_all/tot_spending_all_main
bysort year Main: egen infl_main=sum(s_item*infl)

foreach i in spending_all spending_incq1 spending_incq2 spending_incq3 spending_incq4 spending_incq5 {
bysort year: egen double tot_`i'=sum(`i')
gen s_`i'=`i'/tot_`i'
}

egen id=group(series_id)
tsset id year

foreach i in spending_all spending_incq1 spending_incq2 spending_incq3 spending_incq4 spending_incq5 {
gen so_`i'=L.s_`i'
}

* Compute price indices
gen price_ratio=infl_main+1
replace infl=infl_main
foreach i in all incq1 incq2 incq3 incq4 incq5 {
* laspeyres * paasche
bysort year: egen laspeyres_`i'=sum(infl*so_spending_`i')
bysort year: egen paasche_`i'=sum(infl*s_spending_`i')
* tornqvist
gen weight_`i'=1/2*(s_spending_`i'+so_spending_`i')
gen double numerator_temp_`i'=weight_`i'*log(price_ratio)
bysort year: egen double numerator_`i'=sum(numerator_temp_`i')
gen double tornqvist_`i'=exp(numerator_`i')
* express as inflation in %
replace laspeyres_`i'=laspeyres_`i'*100
replace paasche_`i'=paasche_`i'*100
replace tornqvist_`i'=(tornqvist_`i'-1)*100
}

keep year las* paa* tor*
duplicates drop

drop if year==2004
collapse (mean) *
drop *_all 
reshape long laspeyres_incq paasche_incq tornqvist_incq, i(year) j(income_quintile)
drop year

keep if income_quintile==1 | income_quintile==5
gsort - inc
gen infl_diff=tornqvist_incq-tornqvist_incq[_n-1]
drop if missing(infl_diff)
keep infl_diff
gen AggregationLevel="Main Categories"
merge 1:1 Aggregation using "$resultspath\Table3.dta"
drop _merge
order Agg N infl_diff
egen infl_diff_full=max(infl_diff)
gen infl_diff_frac = infl_diff/infl_diff_full*100
format infl_diff_frac %8.2f
format infl_diff %8.4f
drop infl_diff_full
gsort - N
save "$resultspath\Table3.dta", replace
